# -*- coding:utf8 -*-
import os

"""
1.计算某用户`223.73.89.192`访问次数
2.计算所有用户的访问次数
"""
base_dir = os.path.dirname(os.path.abspath(__file__))
data_file = base_dir + "/" + "access.log"
f = open(file=data_file,mode="r")
count = 0
for line in f:
    ip_addr = line.split()[0]
    if ip_addr == "223.73.89.192":
        count += 1

#用户`223.73.89.192`访问次数
print("223.73.89.192访问次数为: ", count)
user_dict = {}
f.seek(0)
#统计各个用户访问次数，由大到小排序输出
for line in f:
    ip_addr = line.split()[0]
    if ip_addr in user_dict.keys():
        user_dict[ip_addr] += 1
    else:
        user_dict[ip_addr] = 1
#使用冒泡排序
for user in user_dict.keys():
    print(user,user_dict[user])
f.close()